package mhys.Dao;
import mhys.Entity.Teacher;
import mhys.Util.DBUtil;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TeaDao extends BaseDao{
    //查询全部教师
    public List<Teacher> getTeachers(){
        String sql = "select t_id,t_name,t_number,sex,classes,phone from teacher";
        List<Teacher> teachers = new ArrayList<Teacher>();
        try {
            res = select(sql);
            while (res.next()){
                Integer t_id = res.getInt("t_id");
                String t_name = res.getString("t_name");
                String t_number = res.getString("t_number");
                String sex = res.getString("sex");
                String classes = res.getString("classes");
                String phone = res.getString("phone");
                Teacher teacher = new Teacher(t_id, t_name, t_number, sex, classes, phone);
                teachers.add(teacher);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(res,stmt,conn);
        }
        return teachers;
    }

    //按名字查询教师
    public List<Teacher> getTeaByName(String name){
        String sql = "select t_id,t_name,t_number,sex,classes,phone from teacher where t_name like ?";
        List<Teacher> teachers = new ArrayList<Teacher>();
        try {
            res = select(sql,name);
            while (res.next()){
                Integer t_id = res.getInt("t_id");
                String t_name = res.getString("t_name");
                String t_number = res.getString("t_number");
                String sex = res.getString("sex");
                String classes = res.getString("classes");
                String phone = res.getString("phone");
                Teacher teacher = new Teacher(t_id, t_name, t_number, sex, classes, phone);
                teachers.add(teacher);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(res,stmt,conn);
        }
        return teachers;
    }
    //修改教师信息
    public int updateTea(Teacher teacher){
        String sql = "update teacher set t_name = ?,t_number = ?,sex = ?,classes = ?,phone =? WHERE t_id = ?";
        return insertUpdateDelete(sql,teacher.getT_name(),teacher.getT_number(),teacher.getSex(),teacher.getClasses(),teacher.getPhone(),teacher.getT_id());
    }

    //删除教师
    public int delTea(Integer id){
        String sql = "delete from teacher where t_id = ?";
        return insertUpdateDelete(sql,id);
    }
    public int addTea(Teacher teacher){
        String sql = "insert into teacher values(null,?,?,?,?,?)";
        return insertUpdateDelete(sql,teacher.getT_name(),teacher.getT_number(),teacher.getSex(),teacher.getClasses(),teacher.getPhone());
    }
    public static void main(String[] args) {
        TeaDao teaDao = new TeaDao();
        Teacher teacher = new Teacher(null,"金敏","123312","男","信息创新22-7","123124124");
        System.out.println(teaDao.updateTea(teacher));
    }
}
